package com.gloaba.resource.api.service;

import cn.hutool.core.codec.Base64;
import com.gloaba.common.core.domain.R;
import com.gloaba.common.core.exception.ServiceException;
import com.gloaba.common.core.utils.StringUtils;
import com.gloaba.resource.api.OssRemoteService;
import com.gloaba.resource.api.domain.SysFile;
import com.gloaba.resource.api.domain.UploadFileVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * 对象存储内部调用处理
 *
 * @author Zane
 * @date 2023-11-09
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OssRemoteServiceImpl implements OssRemoteService {

    private final FileRemoteService fileRemoteService;

    public R<SysFile> upload(String fileName, String contentType, String path, byte[] fileBytes, Boolean useOriginalName){
        return fileRemoteService.upload(buildUploadFileVo(fileName,contentType,path,Base64.encode(fileBytes), useOriginalName));
    }

    public R<SysFile> upload(String fileName, String contentType, String path, InputStream fileBytes, Boolean useOriginalName){
        return fileRemoteService.upload(buildUploadFileVo(fileName,contentType,path,Base64.encode(fileBytes), useOriginalName));
    }

    public R<SysFile> upload(String fileName, String contentType, String path, String base64Data, Boolean useOriginalName){
        return fileRemoteService.upload(buildUploadFileVo(fileName,contentType,path,base64Data, useOriginalName));
    }

    public R<SysFile> upload(File file, String contentType, String path, Boolean useOriginalName){
        if(StringUtils.isBlank(contentType)){
            try {
                contentType = Files.probeContentType(file.toPath());
            } catch (IOException e) {
                log.error("Read File[%s] ContentType Error",file.getName());
            }
        }
        return fileRemoteService.upload(buildUploadFileVo(file.getName(),contentType,path,Base64.encode(file), useOriginalName));
    }

    public R<SysFile> upload(MultipartFile file, String path, Boolean useOriginalName){
        byte[] fileBytes ;
        try
        {
            fileBytes = file.getBytes();
        }catch(IOException ex){
            throw new ServiceException("读取文件失败 ");
        }
        return fileRemoteService.upload(buildUploadFileVo(file.getOriginalFilename(),file.getContentType(),path,Base64.encode(fileBytes), useOriginalName));
    }

    private UploadFileVo buildUploadFileVo(String fileName, String contentType, String path, String base64Data, Boolean useOriginalName){
        UploadFileVo uploadFileVo = new UploadFileVo();
        uploadFileVo.setFileName(fileName);
        uploadFileVo.setContentType(contentType);
        uploadFileVo.setPath(path);
        uploadFileVo.setBase64Data(base64Data);
        uploadFileVo.setUseOriginalFileName(useOriginalName);
        return uploadFileVo;
    }
}
